<html>
<body>

<?php include 'mainheader.php'; ?>
<?php include 'databaseutil.php'; ?>


<?php

function swapValues( &$a, &$b )
{
	$temp = $a;
	$a = $b;
	$b = $temp;
}

function orderByPercent( &$perc, &$asset, &$firstDate, &$lastDate )
{
	for( $i = 0; $i < 12; $i = $i + 1 )
	{
		for( $j = $i + 1; $j < 12; $j = $j + 1 )
		{
			$curPerc = $perc[$i];
			if( $perc[$j] > $curPerc )
			{
				// swap
				swapValues( $perc[$j], $perc[$i] );
				swapValues( $asset[$j], $asset[$i] );
				swapValues( $firstDate[$j], $firstDate[$i] );
				swapValues( $lastDate[$j], $lastDate[$i] );
			}
		}
	} 
}

function getMaxValue( $valuesArray )
{
	// descobre tamanho do array
	$arrlength = count($valuesArray);

	$maxValue = $valuesArray[0];
	$maxIndex = $valuesArray[0];
	for( $i = 0; $i < $arrlength; $i++ )
	{
		if( $valuesArray[$i] > $maxValue )
		{
			$maxValue = $valuesArray[$i];
			$maxIndex = $i;
		}
	}

	return $maxIndex;
}

function getMinValue( $valuesArray )
{
	// descobre tamanho do array
	$arrlength = count($valuesArray);

	$minValue = $valuesArray[0];
	$minIndex = $valuesArray[0];
	for( $i = 0; $i < $arrlength; $i++ )
	{
		if( $valuesArray[$i] < $minValue )
		{
			$minValue = $valuesArray[$i];
			$minIndex = $i;
		}
	}

	return $minIndex;
}

// Funcoes de consulta com o banco de dados
function getOldestAssetsValues()
{
	// Connect do database
	$con = mysqli_connect("localhost", "root", "assetsdbpass", "assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$sql = mysqli_query($con, "SELECT * FROM assets ORDER BY year ASC, month ASC" );

	// Divide pelo numero total de ativos
	$row = mysqli_fetch_array($sql);

	// Pega os valores do mes/ano mais antigos do sistema
	$initMonth = $row['month'];
	$initYear = $row['year'];

	// Recupera os valores dos ativos mais antigos do sistema
	$oldestAssetsValues = array();
	$oldestAssetsValues[0] = getAssetValue( "2Qtos C. Grande", $initMonth, $initYear );
	$oldestAssetsValues[1] = getAssetValue( "2Qtos Curicica", $initMonth, $initYear );
	$oldestAssetsValues[2] = getAssetValue( "GOLD", $initMonth, $initYear );
	$oldestAssetsValues[3] = getAssetValue( "SILVER", $initMonth, $initYear );
	$oldestAssetsValues[4] = getAssetValue( "DOW", $initMonth, $initYear );
	$oldestAssetsValues[5] = getAssetValue( "iBovespa", $initMonth, $initYear );
	$oldestAssetsValues[6] = getAssetValue( "PETR4", $initMonth, $initYear );
	$oldestAssetsValues[7] = getAssetValue( "VALE5", $initMonth, $initYear );
	$oldestAssetsValues[8] = getAssetValue( "OGXP3", $initMonth, $initYear );
	$oldestAssetsValues[9] = getAssetValue( "USIM5", $initMonth, $initYear );
	$oldestAssetsValues[10] = getAssetValue( "Dollar", $initMonth, $initYear );
	$oldestAssetsValues[11] = getAssetValue( "Euro", $initMonth, $initYear );

	// Fecha o banco
	mysqli_close($con);

	return $oldestAssetsValues;

} 

function getNewestAssetsValues()
{
	// Connect do database
	$con = mysqli_connect("localhost", "root", "assetsdbpass", "assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$sql = mysqli_query($con, "SELECT * FROM assets ORDER BY year DESC, month DESC" );

	// Divide pelo numero total de ativos
	$row = mysqli_fetch_array($sql);

	// Pega os valores do mes/ano mais antigos do sistema
	$finalMonth = $row['month'];
	$finalYear = $row['year'];

	// Recupera os valores dos ativos mais antigos do sistema
	$newestAssetsValues = array();
	$newestAssetsValues[0] = getAssetValue( "2Qtos C. Grande", $finalMonth, $finalYear );
	$newestAssetsValues[1] = getAssetValue( "2Qtos Curicica", $finalMonth, $finalYear );
	$newestAssetsValues[2] = getAssetValue( "GOLD", $finalMonth, $finalYear );
	$newestAssetsValues[3] = getAssetValue( "SILVER", $finalMonth, $finalYear );
	$newestAssetsValues[4] = getAssetValue( "DOW", $finalMonth, $finalYear );
	$newestAssetsValues[5] = getAssetValue( "iBovespa", $finalMonth, $finalYear );
	$newestAssetsValues[6] = getAssetValue( "PETR4", $finalMonth, $finalYear );
	$newestAssetsValues[7] = getAssetValue( "VALE5", $finalMonth, $finalYear );
	$newestAssetsValues[8] = getAssetValue( "OGXP3", $finalMonth, $finalYear );
	$newestAssetsValues[9] = getAssetValue( "USIM5", $finalMonth, $finalYear );
	$newestAssetsValues[10] = getAssetValue( "Dollar", $finalMonth, $finalYear );
	$newestAssetsValues[11] = getAssetValue( "Euro", $finalMonth, $finalYear );

	// Fecha o banco
	mysqli_close($con);

	return $newestAssetsValues;

} 

function getBestWorstAssetPerformance( &$bestIndex, &$worstIndex, &$oldValues, &$newValues, &$percentValues )
{
	$oldValues = array();
	$oldValues = getOldestAssetsValues();

	$newValues = array();
	$newValues = getNewestAssetsValues();

	// Calcula o delta entre as valorizacoes
	$percentValues = array();
	$percentValues[0] = ($newValues[0] - $oldValues[0]) * 100 / $oldValues[0];
	$percentValues[1] = ($newValues[1] - $oldValues[1]) * 100 / $oldValues[1];
	$percentValues[2] = ($newValues[2] - $oldValues[2]) * 100 / $oldValues[2];
	$percentValues[3] = ($newValues[3] - $oldValues[3]) * 100 / $oldValues[3];
	$percentValues[4] = ($newValues[4] - $oldValues[4]) * 100 / $oldValues[4];
	$percentValues[5] = ($newValues[5] - $oldValues[5]) * 100 / $oldValues[5];
	$percentValues[6] = ($newValues[6] - $oldValues[6]) * 100 / $oldValues[6];
	$percentValues[7] = ($newValues[7] - $oldValues[7]) * 100 / $oldValues[7];
	$percentValues[8] = ($newValues[8] - $oldValues[8]) * 100 / $oldValues[8];
	$percentValues[9] = ($newValues[9] - $oldValues[9]) * 100 / $oldValues[9];
	$percentValues[10] = ($newValues[10] - $oldValues[10]) * 100 / $oldValues[10];
	$percentValues[11] = ($newValues[11] - $oldValues[11]) * 100 / $oldValues[11];

	$bestIndex = getMaxValue( $percentValues );
	$worstIndex = getMinValue( $percentValues );
}


// Main

$assetsArray = array( "2Qtos C. Grande", "2Qtos Curicica", "GOLD", "SILVER", "DOW", "iBovespa", "PETR4", "VALE5", "OGXP3", "USIM5", "Dollar", "Euro" );
$monthArray = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");

// Descobre as datas usadas na compilacao dos dados
$oldestMonth = -1;
$oldestYear = -1;
$newestMonth = -1;
$newestYear = -1;
getOldestDate( $oldestMonth, $oldestYear );
getNewestDate( $newestMonth, $newestYear );

echo "<br><br>Number of months entered: " . getNumberOfRecords() / 12;

// Evita divisao por zero
if( getNumberOfRecords() == 0 )
	return;

echo "<br>Last date entered: " . $monthArray[$newestMonth] . "/" . $newestYear;
echo "<br>Number of Records: " . getNumberOfRecords();

// Descobre os valores antigos, novos e o percentual de valorizacao entre eles
$best = -1;
$worst = -1;
$oldValues = array();
$newValues = array();
$percentValues = array();
getBestWorstAssetPerformance( $best, $worst, $oldValues, $newValues, $percentValues );

// Exibe resultado dos ativos que melhor/pior se destacaram
echo "<br>The best performance asset is: " . $assetsArray[$best];
echo "<br>The worst performance asset is: " . $assetsArray[$worst];

?>

</body>
</html>
